//
//  UIViewController+ScanQR_Logic.m
//  ChangChat
//
//  Created by Cocoa on 2019/10/15.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "UIViewController+ScanQR_Logic.h"
#import "DIYScanVC.h"
#import "FriendInfoListVC.h"
#import <LKDBHelper/LKDBHelper.h>
#import "SessionItem.h"
#import "ChatContentVC.h"
#import "YBApiManager+Chat.h"

@implementation UIViewController (ScanQR_Logic)

- (void)qr_showScanQRVC {
    DIYScanVC *scanVC = [DIYScanVC new];
    
    
    scanVC.scanResult = ^(NSString *result) {
        NSDictionary *resultDic = [result mj_JSONObject];
        NSString *qr_type = [resultDic objectForKey:@"qr_type"];
        if ([qr_type isEqualToString:@"group"] == YES) {
            GroupItem *group = [GroupItem mj_objectWithKeyValues:resultDic];
            [self qr_showGroupChatWithGroup:group];
        } else if ([qr_type isEqualToString:@"user"] == YES) {
            [self qr_showUserInfoWithFriend:[FriendItem mj_objectWithKeyValues:resultDic]];
        }
    };
    [self.navigationController pushViewController:scanVC animated:YES];
}
- (void)qr_showGroupChatWithGroup:(GroupItem *)group {
    __block GroupItem *originGroup = nil;
    [kUsermanager.groupChatArray enumerateObjectsUsingBlock:^(GroupItem  *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if ([group.group_id isEqualToString:obj.group_id] == YES) {
            originGroup = obj;
            *stop = YES;
        }
    }];
    SessionItem *session = nil;
    if (originGroup) {
        session = [SessionItem searchSingleWithWhere:@{@"group_id": originGroup.group_id} orderBy:nil];
        if (session == nil) {
            session = [SessionItem new];
            session.group_id = group.group_id;
            session.groupIcon = group.avatar;
            session.groupName = group.name;
            [SessionItem insertToDB:session];
            [[NSNotificationCenter defaultCenter] postNotificationName:notification_NewSession_created object:session];

        }
        
        ChatContentVC *chatvc = [ChatContentVC new];
        chatvc.session = session;
        [self.navigationController pushViewController:chatvc animated:YES];
        chatvc.session.unread_msg_count = 0;
    } else {
        @weakify(self);
        [MBProgressHUD showActivityMessageInView:@"加入群聊"];
        [[YBApiManager.shareManager addGroupUserWithUserIds:kUsermanager.me.ID groupId:group.group_id] subscribeNext:^(id  _Nullable x) {
            [MBProgressHUD hideHUD];
            @strongify(self);
            SessionItem *session = [SessionItem new];
            session.group_id = group.group_id;
            session.groupIcon = group.avatar;
            session.groupName = group.name;
            [SessionItem insertToDB:session];
            [kUsermanager updateGroupChatList];
            ChatContentVC *chatVC = [ChatContentVC new];
            chatVC.session = session;
            [[NSNotificationCenter defaultCenter] postNotificationName:notification_NewSession_created object:session];
            [self.navigationController pushViewController:chatVC animated:YES];
            
        } error:^(NSError * _Nullable error) {
            [MBProgressHUD hideHUD];
            if ([error isServerError]) {
                [MBProgressHUD showTipMessageInView:@"加入群聊失败，请重新扫码" timer:1.0f];
            } else {
                [MBProgressHUD showTipMessageInView:@"网络错误，请重试" timer:1.0f];
            }
        }];
    }
    
}
- (void)qr_showUserInfoWithFriend:(FriendItem *)user {
    FriendInfoListVC *VC = [FriendInfoListVC new];
    VC.user = user;
    VC.from = @"0";
    VC.listType = 2;
    
    [self.navigationController pushViewController:VC animated:YES];
}
@end
